import 'package:get/get.dart';
import 'dart:convert';
import 'model.dart';
import '../apis/app.dart';

class EssayLogic extends GetxController {


  // 列表数据
  final goods = <EssayModel>[].obs;

  @override
  void onInit() {
    super.onInit();
    getEssays();
  }

  // 获取列表
  getEssays() async {
    try {
      var res = await essayApi.getEssays();
      print('Response data: $res'); // 打印原始响应
      
      if (res is String) {
        Map<String, dynamic> jsonData = json.decode(res);
        if (jsonData['data'] is List) {
          var rawList = List<Map<String, dynamic>>.from(jsonData['data'][0]);
          var list = rawList.map((item) => EssayModel.fromJson(item)).toList();
          goods.value = list;
          print('Converted goods: $goods');
        }

      } else if (res is Map && res['data'] is List) {
        var rawList = List<Map<String, dynamic>>.from(res['data'][0]);
        var list = rawList.map((item) => EssayModel.fromJson(item)).toList();
        goods.value = list;
      } else {
        print('Unexpected response type: ${res.runtimeType}');
        goods.value = [];
      }
    } catch (e) {
      print('Error parsing data: $e');
      goods.value = [];
    }
  }

}
